package com.ayh.campusfun.controller.api.v1;

import cn.hutool.core.bean.BeanUtil;
import com.ayh.campusfun.common.CommonResult;
import com.ayh.campusfun.common.PageResult;
import com.ayh.campusfun.common.core.jwt.JWTUtils;
import com.ayh.campusfun.common.utils.PraMap;
import com.ayh.campusfun.entity.*;
import com.ayh.campusfun.service.*;
import com.ayh.campusfun.vo.FunClassifyVO;
import com.ayh.campusfun.vo.ShareVO;
import com.ayh.campusfun.vo.TopicClassifyDataVO;
import com.ayh.campusfun.vo.TopicVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 话题、趣事分类表(TopicOrFunClassify)表控制层
 *
 * @author ayh
 * @since 2021-03-20 20:42:42
 */
@RestController
@RequestMapping("topicOrFunClassify")
public class TopicOrFunClassifyController {
    /**
     * 服务对象
     */
    @Autowired
    private TopicOrFunClassifyService topicOrFunClassifyService;

    @Autowired
    private FunShareService funShareService;

    @Autowired
    private TopicShareService topicShareService;

    @Autowired
    private AttentionService attentionService;

    @Autowired
    private LikeFunTopicService likeFunTopicService;

    @Resource
    private JWTUtils jwtUtils;


    @GetMapping(value = "/getFunClassify/{queryType}")
    public CommonResult getFunClassify(
            HttpServletRequest request,
            @PathVariable("queryType") Integer queryType
    ) {

        String uid = jwtUtils.getUidFromRequest(request);

        Map<String, Object> map = new HashMap<>();
        map.put("classifyType", 1);
        List<TopicOrFunClassify> listByParam = topicOrFunClassifyService.findListByParam(map);
        map.clear();
        map.put("limit", 10);
        map.put("page", 1);
        PageResult funSharePageResult;
        if (queryType == 1) {
            map.put("funClassifyId", 1);
            map.put("likeNumGT",100);
            map.put("order", "send_time desc, id desc");
            funSharePageResult = funShareService.findAll(map);

            List<FunShare> funShareList = funSharePageResult.getData();
            Map<String, Object> queryMap = new HashMap<>();
            funSharePageResult.setData(funShareList.stream()
                    .map(v -> {
                        ShareVO shareVO = new ShareVO();
                        BeanUtil.copyProperties(v, shareVO);
                        boolean isClick = false;
                        boolean isAttention = false;
                        queryMap.put("uid", uid);
                        queryMap.put("funId", v.getId());
                        queryMap.put("funType", 1);
                        LikeFunTopic likeFunTopic = likeFunTopicService.findByParam(queryMap);
                        queryMap.clear();
                        queryMap.put("uid", uid);
                        queryMap.put("attentionId", v.getUid());
                        Attention attention = attentionService.findByParam(queryMap);
                        if (likeFunTopic != null) {
                            isClick = true;
                        }
                        if (attention != null) {
                            isAttention = true;
                        }
                        shareVO.setIsClick(isClick);
                        shareVO.setIsAttention(isAttention);
                        return shareVO;
                    }).collect(Collectors.toList()));

        } else {
            map.put("topicClassifyId", 8);
            funSharePageResult = topicShareService.findAll(map);

            List<TopicShare> topicVOList = funSharePageResult.getData();
            Map<String, Object> queryMap = new HashMap<>();

            funSharePageResult.setData(topicVOList.stream()
                    .map(v -> {
                        TopicVO topicVO = new TopicVO();
                        BeanUtil.copyProperties(v, topicVO);
                        boolean isClick = false;
                        boolean isAttention = false;
                        queryMap.put("uid", uid);
                        queryMap.put("funId", v.getId());
                        queryMap.put("funType", 2);
                        LikeFunTopic likeFunTopic = likeFunTopicService.findByParam(queryMap);
                        queryMap.clear();
                        queryMap.put("uid", uid);
                        queryMap.put("attentionId", v.getUid());
                        Attention attention = attentionService.findByParam(queryMap);
                        if (likeFunTopic != null) {
                            isClick = true;
                        }
                        if (attention != null) {
                            isAttention = true;
                        }
                        topicVO.setIsClick(isClick);
                        topicVO.setIsAttention(isAttention);
                        return topicVO;
                    }).collect(Collectors.toList()));
        }

        return CommonResult.success(FunClassifyVO.buildList(listByParam, funSharePageResult));
    }

    @RequestMapping("/topicClassify")
    public CommonResult getTopicClassify(@RequestParam(defaultValue = "1")Integer page,
                                         @RequestParam(defaultValue = "5")Integer limit,
                                         @RequestParam(defaultValue = "2")Integer classifyType) {
        PageResult<TopicOrFunClassify> all = topicOrFunClassifyService.findAll(
                PraMap.create()
                        .put("page", page)
                        .put("limit", limit)
                        .put("classifyType", classifyType));
        return CommonResult.success(all);
    }

    @GetMapping("/getFunClassifyList")
    public CommonResult getFunClassifyList() {
        PageResult<TopicOrFunClassify> all = topicOrFunClassifyService.findAll(
                PraMap.create()
                        .put("page", 1)
                        .put("limit", 10)
                        .put("classifyType", 1));
        return CommonResult.success(all);
    }

    @GetMapping("/topicClassifyData")
    public CommonResult getTopicClassifyData(){
        List<TopicOrFunClassify> listByParam = topicOrFunClassifyService.findListByParam(
                PraMap.create()
                        .put("page", 0)
                        .put("limit", 3)
                        .put("classifyType", 2));
        List<TopicClassifyDataVO> topicClassifyDataVOS = new ArrayList<>();
        listByParam.forEach(item->{
            List<TopicShare> topicShareList = topicShareService.findListByParam(
                    PraMap.create()
                            .put("page", 0)
                            .put("limit", 5)
                            .put("topicClassifyId", item.getId())
            );
            TopicClassifyDataVO build = new TopicClassifyDataVO();
            BeanUtil.copyProperties(item,build);
            PageResult pageResult = new PageResult<>();
            pageResult.setData(topicShareList);
            build.setTopicShareList(pageResult);
            topicClassifyDataVOS.add(build);
        });
        return CommonResult.success(topicClassifyDataVOS);
    }

    @GetMapping("/topicClassifyList")
    public CommonResult getTopicClassifyList(){
        List<TopicOrFunClassify> listByParam = topicOrFunClassifyService.findListByParam(
                PraMap.create()
                        .put("classifyType", 2));
        List<TopicClassifyDataVO> topicClassifyDataVOS = new ArrayList<>();
        listByParam.forEach(item->{
            TopicClassifyDataVO topicClassifyDataVO = new TopicClassifyDataVO();
            BeanUtil.copyProperties(item, topicClassifyDataVO);
            topicClassifyDataVOS.add(topicClassifyDataVO);
        });
        List<TopicShare> topicShareList = topicShareService.findListByParam(
                PraMap.create()
                        .put("page", 0)
                        .put("limit", 15)
                        .put("topicClassifyId", topicClassifyDataVOS.get(0).getId())
        );
        PageResult pageResult = new PageResult<>();
        pageResult.setData(topicShareList);
        topicClassifyDataVOS.get(0).setTopicShareList(pageResult);
        return CommonResult.success(topicClassifyDataVOS);
    }
}
